/**
 * 时间格式化
 * @param { String } time 当前时间
 * @param { String } type 0-精确到日 1-精确到秒 
 */
const formatting = (val, format = 'YYYY-MM-DD') => {
	if (!val) return '';
	const date = new Date(val);
	const year = date.getFullYear();
	const month = String(date.getMonth() + 1).padStart(2, '0');
	const day = String(date.getDate()).padStart(2, '0');

	let formatted = format;
	formatted = formatted.replace('YYYY', year);
	formatted = formatted.replace('MM', month);
	formatted = formatted.replace('DD', day);
	const hours = String(date.getHours()).padStart(2, '0');
	const minutes = String(date.getMinutes()).padStart(2, '0');
	const seconds = String(date.getSeconds()).padStart(2, '0');
	formatted = formatted.replace('HH', hours);
	formatted = formatted.replace('mm', minutes);
	formatted = formatted.replace('ss', seconds);
	return formatted;
}
/**
 * 文字轻提示
 * @param { String } 提示文字 
 * @param { Number } icon 提示icon
 * @param { Number } duration 提示时间
 */
const alert = (str, icon = 0, duration = 1500) => {
	uni.showToast({
		title: str + '',
		icon: ['none', 'success', 'loading'][icon],
		duration: duration
	})
}
/**
 * loading加载
 * @param { String } str 加载文案
 * @param {*} mask 
 */
const loading = (str = ' ', mask = true) => {
	uni.showLoading({
		title: str,
		mask: mask
	})
}
/**
 * 关闭loading提示框
 */
const loading_h = () => {
	uni.hideLoading()
}


/**
 * 可返回跳转
 * @param { String } url 页面地址
 */
const jump_nav = url => {
	if (!url.startsWith('/')) {
		url = '/' + url;
	}
	uni.navigateTo({
		url: url,
		fail: (err) => {
			uni.switchTab({
				url: url
			});
		}
	});
}
/**
 * 不可返回跳转
 * @param { String } url 页面地址
 */
const jump_red = url => {
	uni.redirectTo({
		url: url
	})
}
/**
 * 清除页面栈跳转
 * @param { String } url 页面地址
 */
const jump_rel = url => {
	uni.reLaunch({
		url: url
	})
}
/**
 * 跳转tabBar页
 * @param { String } url 页面地址
 */
const jump_swi = url => {
	uni.switchTab({
		url: url
	})
}
/**
 * 返回上一页面
 * @param { Number } delta 返回层级
 */
const jump_back = (delta) => {
	var pages = getCurrentPages();
	var prevPage = pages[pages.length - 2];
	if (prevPage == undefined) {
		uni.reLaunch({
			url: '/pages/index/index'
		})
	} else {
		uni.navigateBack();
	}
}
/**
 * 跳转视频号
 * @param { String } spId
 * @param { String } feedId 
 */
const openChannelsActivity = (spId, feedId) => {
	uni.openChannelsActivity({
		finderUserName: spId,
		feedId: feedId,
		success: (res) => {
			console.log('打开成功', res);
		},
		fail: (err) => {
			console.log(err);
		}
	});
}

/**
 * 操作本地缓存
 * @param { String } key 缓存键值
 * @param { String } value 缓存数据
 */
const storage = (key, value) => {
	if (value != null) {
		uni.setStorageSync(key, value)
	} else {
		if (key == null) {
			return uni.getStorageInfoSync()
		} else {
			if (key != '#') {
				if (key[0] == '#') {
					uni.removeStorageSync(key.slice(1))
				} else {
					return uni.getStorageSync(key)
				}
			} else if (key == '#') {
				uni.clearStorageSync()
			}
		}
	}
}
const tool = {
	formatting,
	alert,
	loading,
	loading_h,
	jump_nav,
	jump_red,
	jump_rel,
	jump_swi,
	jump_back,
	storage,
}
export default {
	tool
}